// pages/gallery/order/add/index.js
const UUID = require('../../static/uuid')
const Pay = require('../../static/pay')
Page({
  data: {
    pic_list: [],
    product: {},
    addr: null,
    price: 0,
    freight: 8.0
  },
  onLoad: function (options) {
    let product = tt.getStorageSync('gallery__info');
    let pic_list = tt.getStorageSync('gallery__select');
    let addr = tt.getStorageSync('gallery__addr');
    let price = pic_list.length * parseFloat(product.price)
    price = parseFloat(price.toFixed(2))
    console.log(price)
    this.setData({
      product, pic_list, price, addr
    })
  },
  addAddrs () {
    tt.chooseAddress({
      success: (res) => {
        console.log(res)
        tt.setStorageSync('gallery__addr', res)
        this.setData({
          addr: res
        })
      }
    })
  },
  addOrder () {
    if (!this.data.addr) {
      return tt.showToast({
        title: '请先选择收货地址', icon: 'none'
      })
    }
    let uuid = tt.getStorageSync('gallery__uuid')
    if (!uuid) {
      uuid = UUID()
      tt.setStorageSync('gallery__uuid', uuid)
    }
    tt.request({
      url: 'https://douyinmini.163ar.cn/api/order/add',
      method: 'POST',
      data: {
        product: [{
          name: this.data.product.name,
          thumb: this.data.product.thumb,
          pid: this.data.product._id,
          num: this.data.pic_list.length,
          price: this.data.product.price
        }],
        price: this.data.price,
        freight: this.data.price >= 22 ? 0 : 8,
        // price: 0.01,
        // freight: 0,
        user_name: '小程序相册打印',
        user_id: '5da3d40479a6cd1b8a680b17',
        receipt: {
          name: this.data.addr.userName,
          phone: this.data.addr.telNumber,
          addr: this.data.addr.provinceName + this.data.addr.provinceNamecityName + this.data.addr.provinceNamecountyName + this.data.addr.provinceNamedetailInfo,
        },
        remark: JSON.stringify({
          uuid: uuid,
          piclist: this.data.pic_list
        })
      },
      success: (res) => {
        if (res.data.code || !res.data.data) { // 有错误或者订单ID 为空
          tt.showToast({
            title: '订单创建失败', icon: 'none'
          })
        } else {
          this.orderPay(res.data.data)
        }
      }
    })
  },
  orderPay (id) {
    new Promise((resolve, reject) => {
      tt.request({
        method: 'POST',
        url: 'https://douyinmini.163ar.cn/pay/alipay/createOrder',
        data: {
          id: id,
          openid: 'FX3ihYpXOamuQkuf'
        },
        success: (res) => {
          if (!res.data.code) {
            resolve(res.data)
          } else {
            reject(res.data)
          }
        }
      })
    }).then((orderInfo) => {
      return new Promise((resolve, reject) => {
        Pay(orderInfo, (data) => {
          let { out_order_no } = data
          return new Promise(function(resolve, reject) {
            resolve({ code: 9 })
          })
        }).then(res => {
          tt.request({
            url: 'https://douyinmini.163ar.cn/api/order/call_pay', // 支付成功回调接口
            data: {
              type: 1,
              id: id
            }, //支付结果 暂时模拟
            method: 'POST',
            complete: () => {
              tt.reLaunch({ // 关闭当前页面 跳转到订单列表
                url:'/pages/gallery/order/list/index'
              })
            }
          })
        }).catch(err => {
          console.log(err)
          tt.showToast({
            title: '支付失败', icon: 'none'
          })
          tt.reLaunch({ // 关闭当前页面 跳转到订单列表
            url:'/pages/gallery/order/list/index'
          })
        })
      }).catch(err => {
        console.log(err)
        tt.showToast({
          title: '支付失败', icon: 'none'
        })
        tt.reLaunch({ // 关闭当前页面 跳转到订单列表
          url:'/pages/gallery/order/list/index'
        })
      })
    })
  }
})